//
//  DepoistCheckingVC.swift
//  d11
//
//  Created by peale on 2018/11/1.
//  Copyright © 2018年 Tech_001. All rights reserved.
//

import UIKit

class DepoistCheckingVC: BXBaseVC {
    
    var iconImageView:UIImageView?
    var titleLabel:UILabel?
    var subTitleLabel:UILabel?
    var continueBtn:UIButton?
    var homeBtn:UIButton?
    var pageType:Int? = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.hidesBackButton = true
        self.title = "存款审核"
        self.view.backgroundColor = UIColor.white
        self.createSubView()
    }
    
    func createSubView() -> Void {
        self.iconImageView = UIImageView.init()
        self.iconImageView?.image = imageNamed("icon_depoist_checking")
        self.view.addSubview(self.iconImageView!)
        self.iconImageView?.snp.makeConstraints{(make) in
            make.width.equalTo(167)
            make.centerX.equalToSuperview()
            make.height.equalTo(200)
            make.top.equalToSuperview().offset(33)
        }
        self.titleLabel = UILabel.init()
        self.titleLabel?.text = "您的存款申请已经进入审核队列"
        self.titleLabel?.textColor = UIColor.black
        self.titleLabel?.textAlignment = .center
        self.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
        let attrStr = NSMutableAttributedString(string:(self.titleLabel?.text)!)
        let s = NSString(string:(self.titleLabel?.text)!)//需要这一步。
         let rang = s.range(of: "进入审核" as String)
        attrStr.addAttribute(NSAttributedString.Key.foregroundColor, value: HexRGBAlpha(0xFFCF8D, 1), range: rang)
    
        self.titleLabel?.attributedText = attrStr
        self.view.addSubview(self.titleLabel!)
        self.titleLabel?.snp.makeConstraints{(make) in
            make.width.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(30)
            make.top.equalTo((self.iconImageView?.snp.bottom)!).offset(20)
        }
        self.subTitleLabel = UILabel.init()
        self.subTitleLabel?.text = "预计5分钟之内财务部门会进行处理,请等候...\n如需要继续存款,请您先点击一下操作,反馈本次存款信息。"
        self.subTitleLabel?.textColor =  HexRGBAlpha(0x666666, 1)
        self.subTitleLabel?.textAlignment = .center
        self.subTitleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
        self.subTitleLabel?.numberOfLines = 4
        self.view.addSubview(self.subTitleLabel!)
        let attrStr2 = NSMutableAttributedString(string:(self.subTitleLabel?.text)!)
        let s2 = NSString(string:(self.subTitleLabel?.text)!)//需要这一步。
        let rang2 = s2.range(of: "5分钟之内" as String)
        let rang3 = s2.range(of: "进行处理" as String)
        attrStr2.addAttribute(NSAttributedString.Key.foregroundColor, value: HexRGBAlpha(0xFFCF8D, 1), range: rang2)
        attrStr2.addAttribute(NSAttributedString.Key.foregroundColor, value: HexRGBAlpha(0xFFCF8D, 1), range: rang3)
         self.subTitleLabel?.attributedText = attrStr2
        
        self.subTitleLabel?.snp.makeConstraints{(make) in
            make.width.equalToSuperview().offset(-20)
            make.centerX.equalToSuperview()
            make.height.equalTo(80)
            make.top.equalTo((self.titleLabel?.snp.bottom)!).offset(20)
        }
        
        self.continueBtn  = UIButton.init(type: .custom)
        self.continueBtn?.setTitle("继续存款", for: .normal)
         self.continueBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        self.continueBtn?.setTitleColor(UIColor.white, for: .normal)
        self.continueBtn?.setBackgroundImage(imageNamed("icon_depoist_checking_btn_bg"), for: .normal)
        self.continueBtn?.layer.cornerRadius = 5;
        self.continueBtn?.layer.masksToBounds = true
        self.continueBtn?.addTarget(self, action: #selector(self.actionToDepoistContinue), for: .touchUpInside)
        self.view.addSubview(self.continueBtn!)
        self.continueBtn?.snp.makeConstraints { (make) in
            make.width.equalTo((kScreenWidth-60)/2)
            make.height.equalTo(40)
            make.right.equalTo(self.view.snp.centerX).offset(-10)
            make.top.equalTo((self.subTitleLabel?.snp.bottom)!).offset(20)
        }
        
        self.homeBtn  = UIButton.init(type: .custom)
        let layerGradient = CAGradientLayer()
        layerGradient.colors = [
           HexRGBAlpha(0x6E6E6E, 1).cgColor,
           HexRGBAlpha(0x464646, 1).cgColor
        ]
        layerGradient.frame = CGRect(x: 0, y: 0, width: (kScreenWidth-60)/2, height: 40)
        self.homeBtn?.layer.addSublayer(layerGradient)
        self.homeBtn?.setTitle("返回上级", for: .normal)
        self.homeBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        //self.homeBtn?.setBackgroundImage(imageNamed("icon_depoist_checking_btn_bg2"), for: .normal)
        self.homeBtn?.setTitleColor(UIColor.white, for: .normal)
        self.homeBtn?.layer.cornerRadius = 5
        self.homeBtn?.layer.masksToBounds = true
        self.homeBtn?.addTarget(self, action: #selector(self.actionToDepoistHome), for: .touchUpInside)
        self.view.addSubview(self.homeBtn!)
        self.homeBtn?.snp.makeConstraints { (make) in
            make.width.equalTo((kScreenWidth-60)/2)
            make.height.equalTo(40)
            make.left.equalTo(self.view.snp.centerX).offset(10)
            make.top.equalTo((self.subTitleLabel?.snp.bottom)!).offset(20)
        }
    }
    
   @objc func actionToDepoistContinue() -> Void {
       updateOrderStatus(status: "1")
        
    }
    
    @objc func actionToDepoistHome() -> Void {
        updateOrderStatus(status: "2")
    }
    
    
    //更新订单状态
    func updateOrderStatus(status:String) -> Void {
        self.showLoadHUD(title: "更新中...")
        NetWorkRequest(API.updateDepositStatus(url: "3b32669", status: status, ccToken: API.getCcToken(), vcToken: API.getVcToken()), completion: { (responseData,statusCode,strData) -> (Void) in
             self.dismissLoadHUD()
            DispatchQueue.main.async {
                if status == "1"{
                    let vc =  self.navigationController?.viewControllers[1];
                    self.navigationController?.popToViewController(vc!, animated: true)
                }else  if status == "2"{
                    let vc =  self.navigationController?.viewControllers[1];
                    self.navigationController?.popToViewController(vc!, animated: true)
                }
            }
            
        }, failed: { (faildInfo,reCode,msg) -> (Void) in
            DispatchQueue.main.async {
                self.dismissLoadHUD()
                if reCode == "21002"{
                    UIHelper.displayLoginExpireTip(self)
                }else{
                    UIHelper.alertSimpleMessage(about: "\(faildInfo)", withDescription: "", in: self)
                }
            }
        }) { () -> (Void) in
            DispatchQueue.main.async {
                self.dismissLoadHUD()
                UIHelper.alertSimpleMessage(about: "更新订单状态异常!", withDescription: "", in: self)
            }
        }
    }
}
